1 package edu.jiangxin.apktoolbox.help.settings;
2
3 import edu.jiangxin.apktoolbox.swing.extend.EasyChildTabbedPanel;
4 import edu.jiangxin.apktoolbox.utils.Constants;
5 import org.apache.commons.lang3.ArrayUtils;
6 import org.apache.commons.lang3.Strings;
7
8 import javax.swing.*;
9 import java.awt.*;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.io.Serial;
13
14 public class LookAndFeelPanel extends EasyChildTabbedPanel {
15 @Serial
16 private static final long serialVersionUID = 1L;
17
18 private JPanel optionPanel;
19
20 private JComboBox<String> typeComboBox;
21
22 private JPanel operationPanel;
23
24 static {
25
26 UIManager.installLookAndFeel("Flat Light", "com.formdev.flatlaf.FlatLightLaf");
27 UIManager.installLookAndFeel("Flat Dark", "com.formdev.flatlaf.FlatDarkLaf");
28 UIManager.installLookAndFeel("Flat IntelliJ", "com.formdev.flatlaf.FlatIntelliJLaf");
29 UIManager.installLookAndFeel("Flat Darcula", "com.formdev.flatlaf.FlatDarculaLaf");
30 }
31
32 @Override
33 public void createUI() {
34 BoxLayout boxLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
35 setLayout(boxLayout);
36
37 createOptionPanel();
38 add(optionPanel);
39
40 add(Box.createVerticalStrut(Constants.DEFAULT_Y_BORDER));
41
42 createOperationPanel();
43 add(operationPanel);
44
45 add(Box.createVerticalStrut(15 * Constants.DEFAULT_Y_BORDER));
46 }
47
48 private void createOptionPanel() {
49 optionPanel = new JPanel();
50 optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.X_AXIS));
51
52 JLabel typeLabel = new JLabel("Type:");
53 typeComboBox = new JComboBox<>();
54 typeComboBox.setMaximumSize(new Dimension(Constants.DEFAULT_COMBOBOX_WIDTH, Constants.DEFAULT_COMBOBOX_HEIGHT));
55
56 UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
57 if (ArrayUtils.isEmpty(lookAndFeelInfos)) {
58 typeComboBox.setEnabled(false);
59 } else {
60 typeComboBox.setEnabled(true);
61 String className = conf.getString("look.and.feel.class.name");
62 for (UIManager.LookAndFeelInfo info : lookAndFeelInfos) {
63 typeComboBox.addItem(info.getName());
64 if (Strings.CS.equals(className, info.getClassName())) {
65 typeComboBox.setSelectedItem(info.getName());
66 }
67 }
68 }
69
70 optionPanel.add(typeLabel);
71 optionPanel.add(Box.createHorizontalStrut(Constants.DEFAULT_X_BORDER));
72 optionPanel.add(typeComboBox);
73 }
74
75 private void createOperationPanel() {
76 operationPanel = new JPanel();
77 operationPanel.setLayout(new BoxLayout(operationPanel, BoxLayout.X_AXIS));
78
79 JButton applyButton = new JButton("Apply");
80 applyButton.addActionListener(new ApplyButtonActionListener());
81
82 operationPanel.add(applyButton);
83 }
84
85 private final class ApplyButtonActionListener implements ActionListener {
86 @Override
87 public void actionPerformed(ActionEvent actionEvent) {
88 String name = (String) typeComboBox.getSelectedItem();
89 String className = getLookAndFeelClassNameFromName(name);
90 if (className == null) {
91 logger.warn("className is null");
92 return;
93 }
94 conf.setProperty("look.and.feel.class.name", className);
95 try {
96 UIManager.setLookAndFeel(className);
97 } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
98 logger.error("setLookAndFeel failed, use default instead", e);
99 }
100 SwingUtilities.updateComponentTreeUI(getFrame());
101 getFrame().refreshSizeAndLocation();
102 }
103 }
104
105 private String getLookAndFeelClassNameFromName(String name) {
106 UIManager.LookAndFeelInfo[] lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
107 for (UIManager.LookAndFeelInfo info : lookAndFeelInfos) {
108 if (Strings.CS.equals(name, info.getName())) {
109 return info.getClassName();
110 }
111 }
112 return null;
113 }
114 }